package com.frontop.system.modules.sale.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.frontop.common.domain.UserJurisdiction;
import com.frontop.common.util.JwtUtil;
import com.frontop.common.util.ResultJsonUtil;
import com.frontop.system.modules.sale.model.SaleInfo;
import com.frontop.system.modules.sale.service.SaleInfoService;
import com.frontop.system.modules.sale.vo.SaleInfoVO;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * <p>
 * 营销信息 前端控制器
 * </p>
 *
 * @author YangJia
 * @since 2022-04-24
 */
@RestController
@RequestMapping("/sale-info")
public class SaleInfoController {

    @Autowired
    SaleInfoService saleInfoService;

    @ApiOperation(value = "销售顾问列表")
    @PostMapping("/saleListPage")
    public ResultJsonUtil<Object> saleListPage(@RequestBody(required = false) SaleInfoVO vo,
                                                 Authentication authentication) {
        //获取当前登录用户基本权限信息
        UserJurisdiction userJurisdiction = JwtUtil.getUserJurisdiction(authentication);
        //只能查看子组织和自己组织的销售顾问
        vo.setLowerOrganizationIds(userJurisdiction.getLowerOrganizationIds());
        Page<SaleInfo> page = new Page<>(vo.getPageNo(), vo.getPageSize());
        page = saleInfoService.saleListPage(page,vo);
        return new ResultJsonUtil().success(page);
    }


    @ApiOperation(value = "修改或者添加新的销售顾问信息")
    @PostMapping("/saveOrUpdate")
    public ResultJsonUtil<Object> saveOrUpdate(@RequestBody(required = false) SaleInfo saleInfo,
                                               Authentication authentication) {
        if(saleInfo.getId() == null || saleInfo.getId().equals(0)){
            saleInfo.setCreateTime(new Date());
            saleInfo.setStatus(1);
            String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
            saleInfo.setUuid(uuid);
            //新增默认头像
            saleInfo.setHeadPath("defult.png");
        }else {
            saleInfo.setHeadPath(null);
        }

        saleInfoService.saveOrUpdate(saleInfo);
        return new ResultJsonUtil().success("请求成功");
    }

    @ApiOperation(value = "上传头像")
    @PostMapping("/upSaleHeadImage")
    public ResultJsonUtil<Object> upSaleHeadImage(@RequestParam("file") MultipartFile multipartFile,
                                                  @RequestParam("id") Integer id,
                                                      Authentication authentication) {
        String userHead = saleInfoService.upSaleHeadImage(multipartFile,id);
        return new ResultJsonUtil().success(userHead);
    }


    @ApiOperation(value = "删除顾问信息")
    @GetMapping("/deleteSale/{id}")
    public ResultJsonUtil<Object> deleteSale(@PathVariable("id") Integer id,
                                               Authentication authentication) {
        SaleInfo saleInfo = new SaleInfo();
        saleInfo.setStatus(0);
        saleInfo.setId(id);
        saleInfoService.updateById(saleInfo);
        return new ResultJsonUtil().success("请求成功");
    }

    @ApiOperation(value = "根据企业查询所有顾问列表")
    @GetMapping("/getSaleByOrganization/{organization}")
    public ResultJsonUtil<Object> getSaleByOrganization(@PathVariable("organization") Integer organization,
                                             Authentication authentication) {
        QueryWrapper<SaleInfo> queryWrapper = new QueryWrapper();
        queryWrapper.eq("organization",organization);
        queryWrapper.eq("status",1);
        List<SaleInfo> saleInfoList = saleInfoService.list(queryWrapper);
        return new ResultJsonUtil().success(saleInfoList);
    }



}
